<?php

/* Meta boxes */

function realestate_admin_init(){

	//Price
	add_meta_box('realestate_post_price_meta', 'Real Estate Price', 'realestate_properties_post_price_meta', 'post', 'side', 'high');
	add_meta_box('realestate_post_price_meta', 'Real Estate Price', 'realestate_properties_post_price_meta', 'page', 'side', 'high');

	//School
	add_meta_box('realestate_properties_post_school_meta', 'Real Estate School', 'realestate_properties_post_school_meta', 'post', 'side', 'high');
	add_meta_box('realestate_properties_post_school_meta', 'Real Estate School', 'realestate_properties_post_school_meta', 'page', 'side', 'high');

	//Short Title
	add_meta_box('realestate_post_title_meta', 'Real Estate Short Title', 'realestate_properties_post_title_meta', 'post', 'normal', 'high');
	add_meta_box('realestate_post_title_meta', 'Real Estate Short Title', 'realestate_properties_post_title_meta', 'page', 'normal', 'high');

	//Gallery
	add_meta_box('realestate_post_gallery_meta', 'Real Estate Gallery', 'realestate_properties_post_gallery_meta', 'post', 'normal', 'high');
	add_meta_box('realestate_post_gallery_meta', 'Real Estate Gallery', 'realestate_properties_post_gallery_meta', 'page', 'normal', 'high');

	//Template
	add_meta_box('realestate_properties_page_template_meta', 'Real Estate Template', 'realestate_properties_page_template_meta', 'page', 'normal', 'high');

	//Options
	add_meta_box('realestate_properties_post_options_meta', 'Real Estate Settings', 'realestate_properties_post_options_meta', 'post', 'side', 'high');
	add_meta_box('realestate_properties_post_options_meta', 'Real Estate Settings', 'realestate_properties_post_options_meta', 'page', 'side', 'high');
}
add_action("admin_init", "realestate_admin_init");

function realestate_properties_type_taxonomies(){
	$labels = array(
		'name' => _x('Property Types', 'taxonomy general name'),
		'singular_name' => _x('Property Type', 'taxonomy singular name'),
		'search_items' =>  __('Search Property Types'),
		'popular_items' => __('Popular Property Types'),
		'all_items' => __('All Property Types'),
		'parent_item' => null,
		'parent_item_colon' => null,
		'edit_item' => __('Edit Property Type'),
		'update_item' => __('Update Property Type'),
		'add_new_item' => __('Add New Property Type'),
		'new_item_name' => __('New Property Type Name'),
		'separate_items_with_commas' => __('Separate property types with commas'),
		'add_or_remove_items' => __('Add or remove property types'),
		'choose_from_most_used' => __('Choose from the most used property types'),
		'menu_name' => __('Property Types'),
	);

	register_taxonomy('realestate_properties_type', 'post',array(
		'hierarchical' => false,
		'labels' => $labels,
		'show_ui' => true,
		'update_count_callback' => '_update_post_term_count',
		'query_var' => true,
		'sort' => true,
		'args' => array('orderby' => 'term_order'),
		'rewrite' => array('slug' => 'property'),
	));
}
add_action("init", "realestate_properties_type_taxonomies");

if(!function_exists('get_features')):
	function get_features($args = '', $property = false) {
		global $post;

		if(is_array($property)) $property = (object) $property;

		if(!$property) $property = $post;

		$defaults = array('type' => 'realestate_properties_features', 'format' => 'comma', 'links' => true);
		$args = wp_parse_args( $args, $defaults );

		$features = get_the_terms($property->ID, $args['type']);

		$features_html = array();

		if($features) {
			foreach ($features as $feature)

			if($links)
				array_push($features_html, '<a href="' . get_term_link($feature->slug, $args['type']) . '">' . $feature->name . '</a>');
			else
				array_push($features_html, $feature->name);

			if($args[format] == 'comma') echo implode($features_html, ', ');

			if($args[format] == 'array') return $features_html;

			if($args[format] == 'count') return (count($features) > 0 ? count($features) : false);

			if($args[format] == 'list') echo '<li>' . implode($features_html, '</li><li>') . '</li>';
		}
	}
endif;

function realestate_properties_features_taxonomies(){
	// Add new taxonomy, NOT hierarchical (like tags)
	$labels = array(
		'name' => _x('Property Features', 'taxonomy general name'),
		'singular_name' => _x('Property Features', 'taxonomy singular name'),
		'search_items' =>  __('Search Property Features'),
		'popular_items' => __('Popular Property Features'),
		'all_items' => __('All Property Features'),
		'parent_item' => null,
		'parent_item_colon' => null,
		'edit_item' => __('Edit Property Features'), 
		'update_item' => __('Update Property Features'),
		'add_new_item' => __('Add New Property Features'),
		'new_item_name' => __('New Property Features Name'),
		'separate_items_with_commas' => __('Separate property features with commas'),
		'add_or_remove_items' => __('Add or remove property features'),
		'choose_from_most_used' => __('Choose from the most used property features'),
		'menu_name' => __('Property Features'),
	);

	register_taxonomy('realestate_properties_features', 'post', array(
		'hierarchical' => false,
		'labels' => $labels,
		'show_ui' => true,
		'update_count_callback' => '_update_post_term_count',
		'query_var' => true,
		'sort' => true,
		'args' => array('orderby' => 'term_order'),
		'rewrite' => array('slug' => 'features'),
	));
}

add_action("init", "realestate_properties_features_taxonomies");

function realestate_properties_communitys_taxonomies(){ //features
	// Add new taxonomy, NOT hierarchical (like tags)
	$labels = array(
		'name' => _x( 'Community Features', 'taxonomy general name' ),
		'singular_name' => _x( 'Community Features', 'taxonomy singular name' ),
		'search_items' =>  __( 'Search Community Features' ),
		'popular_items' => __( 'Popular Community Features' ),
		'all_items' => __( 'All Community Features' ),
		'parent_item' => null,
		'parent_item_colon' => null,
		'edit_item' => __( 'Edit Community Features' ), 
		'update_item' => __( 'Update Community Features' ),
		'add_new_item' => __( 'Add New Community Features' ),
		'new_item_name' => __( 'New Community Features Name' ),
		'separate_items_with_commas' => __( 'Separate community features with commas' ),
		'add_or_remove_items' => __( 'Add or remove community features' ),
		'choose_from_most_used' => __( 'Choose from the most used communitys features' ),
		'menu_name' => __( 'Community Features' ),
	);

	register_taxonomy('realestate_properties_communitys','post',array(
		'hierarchical' => false,
		'labels' => $labels,
		'show_ui' => true,
		'update_count_callback' => '_update_post_term_count',
		'query_var' => true,
		'sort' => true,
		'args' => array( 'orderby' => 'term_order' ),
		'rewrite' => array( 'slug' => 'communitys' ),
	));
}

add_action("init", "realestate_properties_communitys_taxonomies");

function realestate_properties_login_logo() {
	echo '<style type="text/css">
		h1 a { background-image:url('.get_bloginfo('template_directory').'/images/logo_small.png) !important; }
	</style>';
}

add_action('login_head', 'realestate_properties_login_logo');

function realestate_properties_post_price_meta()
{
	global $post;
	$custom = get_post_custom($post->ID);
	$price = isset($custom["price"][0]) ? $custom["price"][0] : '';
	
	?>
	<p style="margin-bottom: 22px;">
		<label for="realestate_price">Price</label>
		<input name="realestate_price" id="realestate_price" type="text" value="<?php echo esc_attr($price); ?>" size="20" />
		<small>(ex. 29.99)</small>
	</p>
	<?php
}

function realestate_properties_post_title_meta()
{
	global $post;
	$custom = get_post_custom($post->ID);
	$realestate_title =  isset($custom["realestate_title"][0]) ? $custom["realestate_title"][0] : '';
	
	?>
	<p style="margin-bottom: 22px;">
		<label for="realestate_title">Short Title</label>
		<input name="realestate_title" id="realestate_title" type="text" value="<?php echo esc_attr($realestate_title); ?>" size="100" />
		<small>(ex. Parcel 1234)</small>
	</p>
	<?php
}

function realestate_properties_post_school_meta()
{
	global $post;
	$custom = get_post_custom($post->ID);
	$realestate_school =  isset($custom["realestate_school"][0]) ? $custom["realestate_school"][0] : '';
	
	?>
	<p style="margin-bottom: 22px;">
		<label for="realestate_school">Local School</label>
		<input name="realestate_school" id="realestate_school" type="text" value="<?php echo esc_attr($realestate_school); ?>" size="40" />
		<small>(ex. University of Belize)</small>
	</p>
	<?php
}

//realestate_properties_post_gallery_meta
function realestate_properties_post_gallery_meta() {
	global $post;

	$thumbs = array();
	$custom = get_post_custom($post->ID);

	$featured_image = isset($custom["featured_image"][0]) ? $custom["featured_image"][0] : '';
	$featured_video = isset($custom["featured_video"][0]) ? $custom["featured_video"][0] : '';
	
	$custom["thumbs"] = isset($custom["thumbs"][0]) ? unserialize($custom["thumbs"][0]) : array();
	$thumbnail1 =  isset($custom["thumbs"][0]) ? $custom["thumbs"][0] : '';
	$thumbnail2 =  isset($custom["thumbs"][1]) ? $custom["thumbs"][1] : '';
	$thumbnail3 =  isset($custom["thumbs"][2]) ? $custom["thumbs"][2] : '';
	$thumbnail4 =  isset($custom["thumbs"][3]) ? $custom["thumbs"][3] : '';
	$thumbnail5 =  isset($custom["thumbs"][4]) ? $custom["thumbs"][4] : '';
	$thumbnail6 =  isset($custom["thumbs"][5]) ? $custom["thumbs"][5] : '';
	$thumbnail7 =  isset($custom["thumbs"][6]) ? $custom["thumbs"][6] : '';
	$thumbnail8 =  isset($custom["thumbs"][7]) ? $custom["thumbs"][7] : '';

	?>
	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image">Product Thumbnail #1: </label><br/>
		<input id="realestate_upload_image" type="text" size="90" name="realestate_upload_image" value="<?php echo esc_attr($thumbnail1); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 1st Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image2">Product Thumbnail #2: </label><br/>
		<input id="realestate_upload_image2" type="text" size="90" name="realestate_upload_image2" value="<?php echo esc_attr($thumbnail2); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 2nd Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image3">Product Thumbnail #3: </label><br/>
		<input id="realestate_upload_image3" type="text" size="90" name="realestate_upload_image3" value="<?php echo esc_attr($thumbnail3); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 3rd Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image4">Product Thumbnail #4: </label><br/>
		<input id="realestate_upload_image4" type="text" size="90" name="realestate_upload_image4" value="<?php echo esc_attr($thumbnail4); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 4th Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image5">Product Thumbnail #5: </label><br/>
		<input id="realestate_upload_image5" type="text" size="90" name="realestate_upload_image5" value="<?php echo esc_attr($thumbnail5); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 5th Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image6">Product Thumbnail #6: </label><br/>
		<input id="realestate_upload_image6" type="text" size="90" name="realestate_upload_image6" value="<?php echo esc_attr($thumbnail6); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 6th Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image7">Product Thumbnail #7: </label><br/>
		<input id="realestate_upload_image7" type="text" size="90" name="realestate_upload_image7" value="<?php echo esc_attr($thumbnail7); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 7th Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_upload_image8">Product Thumbnail #8: </label><br/>
		<input id="realestate_upload_image8" type="text" size="90" name="realestate_upload_image8" value="<?php echo esc_attr($thumbnail8); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image for the 8th Product Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="featured_image">Featured Product Alternative Image: </label><br/>
		<input id="featured_image" type="text" size="90" name="featured_image" value="<?php echo esc_attr($featured_image); ?>" />
		<input class="upload_media_button" type="button" value="Upload Image" /><br/>
		<small>(enter an URL or upload an image to use as Featured Product Alternative Image)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="featured_video">Featured Real Estate Video: </label><br/>
		<input id="featured_video" type="text" size="90" name="featured_video" value="<?php echo esc_attr($featured_video); ?>" />
		<input class="upload_media_button" type="button" value="Upload Video" /><br/>
		<small>(enter an URL or upload an video to use as Featured Real Estate Video)</small>
	</p>
	<?php
}


function realestate_properties_page_template_meta($callback_args) {
	global $post;

	$thumbs = array();
	$custom = get_post_custom($post->ID);

	if ($callback_args->post_type == 'page') $realestate_page_template =  isset($custom["realestate_page_template"][0]) ? $custom["realestate_page_template"][0] : ''; ?>

	<?php if ($callback_args->post_type == 'page') { ?>
		<p style="margin-bottom: 22px;">
			<label for="realestate_page_template">Page Type</label>
			<select id="realestate_page_template" name="realestate_page_template">
				<option value="">Product Page</option>
				<option <?php if (htmlspecialchars($realestate_page_template) == 'usual') echo('selected="selected"')?> value="usual">Usual Page</option>
			</select>
		</p>
	<?php }
}

function realestate_properties_post_options_meta() {
	global $post;

	$thumbs = array();
	$custom = get_post_custom($post->ID);

	$realestate_address = isset($custom["realestate_address"][0]) ? $custom["realestate_address"][0] : '';

	$realestate_bedrooms_number = isset($custom["realestate_bedrooms_number"][0]) ? $custom["realestate_bedrooms_number"][0] : '';
	$realestate_bathrooms_number = isset($custom["realestate_bathrooms_number"][0]) ? $custom["realestate_bathrooms_number"][0] : '';
	$realestate_garages_number = isset($custom["realestate_garages_number"][0]) ? $custom["realestate_garages_number"][0] : '';
	$realestate_square_footage = isset($custom["realestate_square_footage"][0]) ? $custom["realestate_square_footage"][0] : '';

	$integrate_gmaps = isset($custom["integrate_gmaps"][0]) ? (bool) $custom["integrate_gmaps"][0] : false;

	?>

	<p style="margin-bottom: 22px;">
		<label for="realestate_bedrooms_number">Number of Bedrooms:</label>
		<input name="realestate_bedrooms_number" id="realestate_bedrooms_number" type="text" value="<?php echo esc_attr($realestate_bedrooms_number); ?>" size="6" />
		<small>(ex. 1)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_bathrooms_number">Number of Bathrooms:</label>
		<input name="realestate_bathrooms_number" id="realestate_bathrooms_number" type="text" value="<?php echo esc_attr($realestate_bathrooms_number); ?>" size="6" />
		<small>(ex. 1)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_garages_number">Number of Garages:</label>
		<input name="realestate_garages_number" id="realestate_garages_number" type="text" value="<?php echo esc_attr($realestate_garages_number); ?>" size="6" />
		<small>(ex. 1)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label for="realestate_square_footage">Square Footage:</label>
		<input name="realestate_square_footage" id="realestate_square_footage" type="text" value="<?php echo esc_attr($realestate_square_footage); ?>" size="6" />
		<small>(ex. 1)</small>
	</p>

	<p style="margin-bottom: 22px;">
		<label class="selectit" for="realestate_show_googlemaps">
			<input type="checkbox" name="realestate_show_googlemaps" id="realestate_show_googlemaps" value=""<?php checked( $integrate_gmaps ); ?> />Integrate google maps
		</label>
	</p>
	<p style="margin-bottom: 22px;">
		<label for="realestate_address">Address</label>
		<input name="realestate_address" id="realestate_address" type="text" value="<?php echo esc_attr($realestate_address); ?>" size="100" />
		<small>(ex. 270 Park Ave. New York)</small>
	</p>
	<?php
}

#
function realestate_properties_save_gallery_meta($post_id){
	global $pagenow;
	if ( 'post.php' != $pagenow ) return $post_id;

	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
		return $post_id;
		
	if (isset($_POST["realestate_upload_image"]) && $_POST["realestate_upload_image"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image"]);
	if (isset($_POST["realestate_upload_image2"]) && $_POST["realestate_upload_image2"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image2"]);
	if (isset($_POST["realestate_upload_image3"]) && $_POST["realestate_upload_image3"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image3"]);
	if (isset($_POST["realestate_upload_image4"]) && $_POST["realestate_upload_image4"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image4"]);
	if (isset($_POST["realestate_upload_image5"]) && $_POST["realestate_upload_image5"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image5"]);
	if (isset($_POST["realestate_upload_image6"]) && $_POST["realestate_upload_image6"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image6"]);
	if (isset($_POST["realestate_upload_image7"]) && $_POST["realestate_upload_image7"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image7"]);
	if (isset($_POST["realestate_upload_image8"]) && $_POST["realestate_upload_image8"] <> '') $thumbs[] = esc_attr($_POST["realestate_upload_image8"]);
	if (!empty($thumbs)) {
		update_post_meta($post_id, "thumbs", $thumbs);
		update_post_meta($post_id, "Thumbnail", $thumbs[0]);
	}
	if (isset($_POST["featured_image"]) && $_POST["featured_image"] <> '') update_post_meta($post_id, "featured_image", esc_attr($_POST["featured_image"]));
	if (isset($_POST["featured_video"]) && $_POST["featured_video"] <> '') update_post_meta($post_id, "featured_video", esc_attr($_POST["featured_video"]));
}
add_action('save_post', 'realestate_properties_save_gallery_meta');

function realestate_save_details($post_id)
{
	global $pagenow;
	if ( 'post.php' != $pagenow ) return $post_id;

	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
		return $post_id;

	//do_action('realestate_save_details_before');

	//@todo hook these later
	if (isset($_POST["realestate_title"]) && $_POST["realestate_title"] <> '') update_post_meta($post_id, "realestate_title", $_POST["realestate_title"]);
	if (isset($_POST["realestate_school"]) && $_POST["realestate_school"] <> '') update_post_meta($post_id, "realestate_school", esc_attr($_POST["realestate_school"]));
    if (isset($_POST["realestate_price"]) && $_POST["realestate_price"] <> '') update_post_meta( $post_id, "price", esc_attr($_POST["realestate_price"]) );
	
	if (isset($_POST["realestate_bedrooms_number"]) && $_POST["realestate_bedrooms_number"] <> '') update_post_meta($post_id, "realestate_bedrooms_number", esc_attr($_POST["realestate_bedrooms_number"]));
	if (isset($_POST["realestate_bathrooms_number"]) && $_POST["realestate_bathrooms_number"] <> '') update_post_meta($post_id, "realestate_bathrooms_number", esc_attr($_POST["realestate_bathrooms_number"]));
	if (isset($_POST["realestate_garages_number"]) && $_POST["realestate_garages_number"] <> '') update_post_meta($post_id, "realestate_garages_number", esc_attr($_POST["realestate_garages_number"]));
	if (isset($_POST["realestate_square_footage"]) && $_POST["realestate_square_footage"] <> '') update_post_meta($post_id, "realestate_square_footage", esc_attr($_POST["realestate_square_footage"]));
	if (isset($_POST["realestate_address"]) && $_POST["realestate_address"] <> '') update_post_meta($post_id, "realestate_address", esc_attr($_POST["realestate_address"]));
	if (isset($_POST["realestate_show_googlemaps"])) update_post_meta($post_id, "integrate_gmaps", 1);

	if (isset($_POST["realestate_page_template"])) update_post_meta($post_id, "realestate_page_template", esc_attr($_POST["realestate_page_template"]));

}
add_action('save_post', 'realestate_save_details');

function realestate_upload_scripts() {
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_register_script('my-upload', get_template_directory_uri().'/js/custom_uploader.js', array('jquery','media-upload','thickbox'));
	wp_enqueue_script('my-upload');
}

function realestate_upload_styles() {
	wp_enqueue_style('thickbox');
}
add_action('admin_print_scripts', 'realestate_upload_scripts');
add_action('admin_print_styles', 'realestate_upload_styles');

?>